import region from '/@/common/region';
import { uploadApi } from '/@/api/sys/upload';
import Trans from '../trans';
import { getFormDefaultValue } from '../func';

class Form {
  static Input = 'Input';
  static InputNumber = 'InputNumber';
  static InputGroup = 'InputGroup';
  static InputTextArea = 'InputTextArea';
  static InputPassword = 'InputPassword';
  static InputCountDown = 'InputCountDown';
  static Select = 'Select';
  static RadioGroup = 'RadioGroup';
  static CheckboxGroup = 'CheckboxGroup';
  static Cascader = 'Cascader';
  static DatePicker = 'DatePicker';
  static MonthPicker = 'MonthPicker';
  static RangePicker = 'RangePicker';
  static TimePicker = 'TimePicker';
  static WeekPicker = 'WeekPicker';
  static Switch = 'Switch';
  static Upload = 'Upload';
  static StrengthMeter = 'StrengthMeter';
  static Slider = 'Slider';
  static Rate = 'Rate';
  static Divider = 'Divider';

  protected _field: string = '';
  protected _label: string = '';
  protected _value: any = '';
  protected _type: string = '';
  protected _type: string = '';
  protected _placeholder: string = '';
  protected _disabled: boolean = false;
  protected _required: boolean = true;
  protected _help: string = '';
  protected _colProps: object = {};
  protected _span: number = 20;
  protected _slot: string = '';
  /**
   * 属性有placeholder,options,onChange,
   *上传类 api
   *
   */
  protected _componentProps: object = {
    placeholder: '',
    options: [],
    onChange: ''
  };

  constructor(field: string, label: string, value: any) {
    this.field = field;
    this.label = label;
    this.value = value === 'undefined' ? '' : value;
  }

  protected _params: object = {};
  set params(params: object) {
    this._params = params ? params : {};
  }
  get params() {
    return {
      label: this.label,
      field: this.field,
      component: this.type,
      required: this.required,
      colProps: this.colProps,
      componentProps: this.componentProps,
      defaultValue: this.value,
      helpMessage: this.help,
      slot: this.slot,
      labelWidth: 120
    };
  }

  set componentProps(props: object) {
    this._componentProps = props;
  }
  get componentProps() {
    if (this.options) {
      this._componentProps.options = this.options;
    }
    this._componentProps.disabled = this.disabled;
    let placeholder = this._placeholder;
    if (placeholder.length === 0) {
      placeholder = '请输入' + this.label;
    }
    this._componentProps.placeholder = placeholder;
    return this._componentProps;
  }
  setOnChange(change) {
    if (typeof change === 'function') {
      this._componentProps.onChange = change;
    }
  }

  set colProps(props: object) {
    this._colProps = props;
  }
  get colProps() {
    this._colProps.span = this.span;
    return this._colProps;
  }

  set span(span: number) {
    this._span = span;
  }
  get span() {
    return this._span;
  }

  set slot(slot: string) {
    this._slot = slot;
  }
  get slot() {
    return this._slot;
  }

  set field(field: string = '') {
    this._field = field;
    return this;
  }
  get field(): string {
    return this._field;
  }

  set label(label: string = '') {
    this._label = label;
  }
  get label() {
    return this._label;
  }

  set value(value: any = '') {
    this._value = value;
  }
  get value() {
    return getFormDefaultValue(this.type, this._value);
  }

  set type(type: string = '') {
    this._type = type;
  }
  get type() {
    return this._type;
  }

  set disabled(disabled: boolean = false) {
    this._disabled = disabled === true ? true : false;
  }
  get disabled() {
    return this._disabled;
  }

  set required(required: boolean = false) {
    this._required = required === true ? true : false;
  }
  get required() {
    return this._required;
  }

  set options(options: Array<any>) {
    this._options = options;
  }
  get options() {
    return this._options;
  }

  set help(message: string) {
    this._help = message;
  }
  get help() {
    return this._help;
  }
  /**
   * 可以设置一个字符串，可以设置一个数组，RangePicker日期范围的时候使用
   * @param {Object} placeholder
   */
  setPlaceHolder(placeholder) {
    this._placeholder = placeholder;
  }

  hidden() {
    this.type = 'hidden';
    this.required = false;
  }
  input() {
    this.type = Form.Input;
  }
  inputGroup(items: Array = [], unit: string = '') {
    this.type = Form.InputGroup;
    this.slot = 'InputRange';
    // 这里给一个值，为了通过效验，使用输入框组的，都取xxx_min xxx_max的值为准
    this.value = -1;
    if (items.length === 0) {
      items = [this.field + '_min', this.field + '_max', unit];
    }
    this._componentProps.items = items;
  }
  password() {
    this.type = Form.InputPassword;
  }
  number() {
    this.type = Form.InputNumber;
  }
  textarea() {
    this.type = Form.InputTextArea;
  }
  editor() {
    this.type = Form.InputTextArea;
    this.slot = 'editor';
  }
  password() {
    this.type = Form.InputPassword;
  }
  countDown() {
    this.type = Form.InputCountDown;
  }
  select(options: Array<object>, trans: boolean = true) {
    this.type = Form.Select;
    this.value = [];
    this.options = trans ? Trans.options(options) : options;
    this._componentProps.showSearch = true;
    this._componentProps.optionFilterProp = 'label';
  }
  selectCheckbox(options: Array<object>, trans: boolean = true) {
    this.type = Form.Select;
    this.options = trans ? Trans.options(options) : options;
    this.value = [];
    this._componentProps.showSearch = true;
    this._componentProps.optionFilterProp = 'label';
    this._componentProps.mode = 'tags';
  }
  radio(options: Array<object>, trans: boolean = true) {
    this.type = Form.RadioGroup;
    this.value = 0;
    this.options = trans ? Trans.options(options) : options;
  }
  checkbox(options: Array<object>, trans: boolean = true) {
    this.type = Form.CheckboxGroup;
    this.value = [];
    this.options = trans ? Trans.options(options) : options;
  }
  cascader(options: Array<object>, trans: boolean = true) {
    this.type = Form.Cascader;
    this.value = [];
    this.options = trans ? Trans.cascader(options) : options;
  }
  datePicker() {
    this.type = Form.DatePicker;
  }
  monthPicker() {
    this.type = Form.MonthPicker;
  }
  rangePicker(placeholder: Array) {
    this.type = Form.RangePicker;
    this.setPlaceHolder(['开始日期', '结束日期']);
    this.value = null;
  }
  timePicker() {
    this.type = Form.TimePicker;
  }
  weekPicker() {
    this.type = Form.WeekPicker;
  }
  switch() {
    this.type = Form.Switch;
  }
  strengthMeter() {
    this.type = Form.StrengthMeter;
  }
  upload(options) {
    this.type = Form.Upload;
    this.value = [];
    if (typeof options === 'object') {
      options.uploadParams.field = this.field;
    }
    this._componentProps = options;
    this._componentProps.api = uploadApi;
  }
  slider() {
    this.type = Form.Slider;
  }
  rate() {
    this.type = Form.Rate;
  }
  region() {
    this.cascader(region,false);
  }
}

export default Form;
